package com.wlyuan.open.domain.source.assembler;

import com.alibaba.fastjson2.JSON;
import com.wlyuan.open.OpenBaseSyncDTO;
import com.wlyuan.open.dto.*;
import com.wlyuan.open.dto.tenant.OpenCompanyTenantSyncDTO;
import com.wlyuan.open.dto.tenant.OpenDriverTenantSyncDTO;
import com.wlyuan.open.dto.tenant.OpenTenantRelationSyncDTO;
import com.wlyuan.open.dto.transport.OpenTransportRelationSyncDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

/**
 * @author lijing
 * @description
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AssemblerConvert {

    private final Map<Class<?>, Function<OpenBaseSyncDTO, ?>> executors = new HashMap<>();

    @PostConstruct
    public void initialize() {
        executors.put(OpenGoodsSyncDTO.class, goods -> GoodsAssembler.assemble((OpenGoodsSyncDTO) goods, null));
        executors.put(OpenCarrierSyncDTO.class, carrier -> CarrierAssembler.assemble((OpenCarrierSyncDTO) carrier));
        executors.put(OpenConsigneeSyncDTO.class, consignee -> ConsigneeAssembler.assemble((OpenConsigneeSyncDTO) consignee, null));
        executors.put(OpenCustomerSyncDTO.class, customer -> CustomerAssembler.assemble((OpenCustomerSyncDTO) customer));
        executors.put(OpenCompanyTenantSyncDTO.class, tenant -> TenantAssembler.assemble((OpenCompanyTenantSyncDTO) tenant));
        executors.put(OpenDriverTenantSyncDTO.class, tenant -> TenantAssembler.assemble((OpenDriverTenantSyncDTO) tenant));
        executors.put(OpenDriverSyncDTO.class, driver -> DriverAssembler.assemble((OpenDriverSyncDTO) driver));
        executors.put(OpenVehicleSyncDTO.class, vehicle -> VehicleAssembler.assemble((OpenVehicleSyncDTO) vehicle));
        executors.put(OpenOrderSyncDTO.class, order -> OrderAssembler.assembler((OpenOrderSyncDTO) order));
        executors.put(OpenTransportSyncDTO.class, transport -> TransportAssembler.assemble((OpenTransportSyncDTO) transport));
        executors.put(OpenContractSyncDTO.class, contract -> ContractAssembler.assemble((OpenContractSyncDTO) contract));
        executors.put(OpenBillSyncDTO.class, finance -> BillAssembler.assemble((OpenBillSyncDTO) finance));
        executors.put(OpenUserSyncDTO.class, user -> UserAssembler.assemble((OpenUserSyncDTO) user));
        executors.put(OpenTenantRelationSyncDTO.class, tenantRelation -> RelationAssembler.assembler((OpenTenantRelationSyncDTO) tenantRelation));
        executors.put(OpenCombineSyncDTO.class, combine -> CombineAssembler.assemble((OpenCombineSyncDTO) combine));
        //executors.put(OpenQuotationSyncDTO.class, quotation -> QuotationAssembler.assemble((OpenQuotationSyncDTO) quotation));
        //executors.put(OpenTemplateSyncDTO.class, template -> TemplateAssembler.assemble((OpenTemplateSyncDTO) template));
        executors.put(OpenPaySyncDTO.class, pay -> PayAssembler.assemble((OpenPaySyncDTO) pay));
        executors.put(OpenRelationSyncDTO.class, relation -> RelationAssembler.assembler((OpenRelationSyncDTO) relation));
        executors.put(OpenTransportRelationSyncDTO.class, relation -> TransportAssembler.assemble((OpenTransportRelationSyncDTO) relation));
        //executors.put(OpenCombineQuotationSyncDTO.class, relation -> QuotationAssembler.assemble((OpenCombineQuotationSyncDTO) relation));
    }

    public <T extends OpenBaseSyncDTO> String convert(T source) {
        var assembler = executors.get(source.getClass());
        return Optional.ofNullable(assembler).map(a -> JSON.toJSONString(a.apply(source))).orElse("");
    }

}
